package com.club.football.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.club.football.dto.EventsTeamsSaveDto;
import com.club.football.dto.PageDto;
import com.club.football.entity.Coach;
import com.club.football.entity.EventsTeams;
import com.club.football.req.EventsTeamsReq;
import com.club.football.service.EventsTeamsService;
import com.club.football.utils.R;
import com.club.football.vo.EventsTeamsListVo;
import com.club.football.vo.EventsTeamsVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

import static com.club.football.common.CommonConstants.*;

/**
 * 赛队Controller
 *
 * @author 1
 * @date 2023/12/28
 */
@RestController
@RequestMapping("/eventsTeams")
public class EventsTeamsController {
    @Resource
    private EventsTeamsService eventsTeamsService;

    @GetMapping("/page")
    public R<Page<EventsTeamsVo>> page(PageDto<EventsTeams> pageDto, EventsTeamsReq eventsTeamsReq) {
        return eventsTeamsService.eventsTeamsPage(pageDto, eventsTeamsReq);
    }

    @GetMapping("/list")
    public R<EventsTeamsListVo> list(EventsTeamsReq eventsTeamsReq) {
        return eventsTeamsService.eventsTeamsList(eventsTeamsReq);
    }

    /**
     * 球队保存
     *
     * @return {@link R}<{@link String}>
     */
    @PostMapping("/save")
    public R<String> save(@RequestBody @Valid EventsTeamsSaveDto saveDto) {
        List<Long> teamIdList = saveDto.getTeamIdList();
        Long eventsId = saveDto.getEventsId();
        eventsTeamsService.lambdaUpdate().eq(EventsTeams::getEventsId, eventsId).remove();
        if (CollUtil.isEmpty(teamIdList)) {
            return R.ok(SAVE_OK_MSG);
        }
        List<EventsTeams> eventsTeamsList = teamIdList.stream().map(e -> new EventsTeams(eventsId, e)).collect(Collectors.toList());
        eventsTeamsService.saveBatch(eventsTeamsList);
        return R.ok(SAVE_OK_MSG);
    }

    /**
     * 删除
     *
     * @param id ID
     * @return {@link R}<{@link String}>
     */
    @DeleteMapping("/remove/{id}")
    public R<String> remove(@PathVariable Long id) {
        eventsTeamsService.removeById(id);
        return R.ok(DEL_OK_MSG);
    }

    /**
     * 信息
     *
     * @param id ID
     * @return {@link R}<{@link Coach}>
     */
    @GetMapping("/info/{id}")
    public R<EventsTeams> info(@PathVariable Long id) {
        return R.ok(eventsTeamsService.getById(id), GET_INFO_OK);
    }
}
